﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Hybrid;

namespace Hybrid
{
    class Leveling
    {
        private Background privateBackground;
        private Timer privateEnemyUpdate;
        private Timer privateEnemyProjTimer;
        private List<Enemy> privateEnemyShips;
        private Bitmaps privateBitmapCollection;
        private Random GlobalRandom = new Random();
        private SoundEffects privateSoundEffect;
        private int MaxHeight = 570;
        private List<PlayerShip> privatePlayerShips;
        private int privateCurrentLevel;
        private SaveGame privateSaveGame = new SaveGame();
        private Graphics privateg;
        private Bitmap privateb;
        private PictureBox privatepb;
        private Timer privateWaitScreen = new Timer();
        private int privateWaitScreenTick = 0;
        private bool privateWait = false;

        private List<int> privateEnemySpeed = new List<int>();
        private List<int> privateEnemyFireProj = new List<int>();
        private List<int> privateEnemyInterval = new List<int>();
        private List<int> privateBackgroundIndex = new List<int>();
        private List<List<int>> Collection = new List<List<int>>();

        public Leveling(int LevelIndex, Background background, Timer gameupdate, Timer enemyupdate, Graphics g, List<Enemy> EnemyShips, Bitmaps BitmapCollection, Timer EnemyProjTimer, SoundEffects SoundEffect, List<PlayerShip> PlayerShips, PictureBox pb, Bitmap b)
        {

            privateEnemySpeed.AddRange(new int[]        { 5, 5, 6, 6, 7, 7, 8, 8, 9, 9 });
            privateEnemyFireProj.AddRange(new int[] { 4000, 3000, 3700, 3300, 3000, 2700, 2300, 2000, 1700, 1300 });
            privateEnemyInterval.AddRange(new int[]     { 5000, 4700, 4300, 4000, 3700, 3300, 3000, 2700, 2300, 2000 });
            for (int i = 0; i < 10; i++)
            {
                Collection.Add(new List<int>() { privateEnemySpeed[i], privateEnemyFireProj[i], privateEnemyInterval[i]});
            }
            this.privatepb = pb;
            this.privateg = g;
            this.privateb = b;
            privateBackground = background;
            privateEnemyUpdate = enemyupdate;
            privateEnemyShips = EnemyShips;
            privateBitmapCollection = BitmapCollection;
            privateEnemyProjTimer = EnemyProjTimer;
            privateSoundEffect = SoundEffect;
            privatePlayerShips = PlayerShips;
            privateCurrentLevel = LevelIndex;
            SetLevel(LevelIndex);
            privateWaitScreen.Interval = 1000;
            privateWaitScreen.Tick += new EventHandler(privateWaitScreen_Tick);
        }

        void privateWaitScreen_Tick(object sender, EventArgs e)
        {
            privateWaitScreenTick++;
            DrawWait();
            if (privateWaitScreenTick == 5)
            {
                privateWait = false;
                privateWaitScreenTick = 0;
                privateWaitScreen.Stop();

            }            
        }

        public void DrawWait()
        {
            privateSoundEffect.Countdown(5 - privateWaitScreenTick);
            privateg.Clear(Color.Black);
            privateBackground.DrawBackground(privateg);
            privateg.DrawString("Level " + (privateCurrentLevel + 1), new Font("Arial", 30, FontStyle.Bold), Brushes.Green, new PointF(300, 190));
            privateg.DrawString("Starting in " + (5 - privateWaitScreenTick), new Font("Arial", 19, FontStyle.Bold), Brushes.Honeydew, new PointF(297, 270));
            privatepb.Image = privateb;
        }

        public void NextLevel()
        {
            privateCurrentLevel++;
            SetLevel(privateCurrentLevel);
            string scores = "";
            if (privatePlayerShips.Count == 1) scores = privatePlayerShips[0].Score.ToString();
            else if (privatePlayerShips.Count == 2) scores = privatePlayerShips[0].Score + "~" + privatePlayerShips[1].Score;
            else if (privatePlayerShips.Count == 3) scores = privatePlayerShips[0].Score + "~" + privatePlayerShips[1].Score + "~" + privatePlayerShips[2].Score;
            privateSaveGame.Save(privateCurrentLevel, privatePlayerShips.Count - 1, scores);
        }

        public bool WaitScreen
        {
            get
            {
                return privateWait;
            }
        }

        public void SetLevel(int LevelIndex)
        {
            privateCurrentLevel = LevelIndex;
            privateBackground.GoToIndex(LevelIndex + 1);
            for (int i = 0; i < privatePlayerShips.Count; i++)
            {
                privatePlayerShips[i].Reset();
            }
            privateEnemyShips.Clear();
            DrawWait();
            privateWait = true;
            privateWaitScreen.Start();
        }

        public int GetLevelIndex
        {
            get
            {
                return privateCurrentLevel;
            }
        }

        public int EnemySpeed
        {
            get
            {

                return Collection[privateCurrentLevel][0];
            }
        }

        public int EnemyFireProj
        {
            get
            {
                return Collection[privateCurrentLevel][1];
            }
        }

        public int EnemyInterval
        {
            get
            {
                return Collection[privateCurrentLevel][2];
            }
        }
    }
}
